package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.AppUtils;
import com.common.lib.bean.ApplicationBean;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.utils.DensityUtil;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.GridViewAdapter;
import com.thinkfit.jy.adapter.ViewPagerAdapter;
import com.thinkfit.jy.utils.BackButtonViewUtils;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.DialogCloseApkViewUtils;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.RunningAppUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AppPopupWindow implements View.OnClickListener {

    private Context mContext;
    WindowManager windowManager;
    WindowManager.LayoutParams layoutParams;
    private View inflate;

    //每页展示的主题个数
    private final int pageSize = 8;
    boolean isFist = true;
    //当前页索引
    private int currentIndex;
    ViewPager viewPager;
    LinearLayout ll_dot;
    ConstraintLayout cl_bg;

    static AppPopupWindow appPopupWindow;
    List<View> viewList;
    ViewPagerAdapter viewPagerAdapter;

    public static AppPopupWindow getInstance(){
        if(appPopupWindow == null){
            appPopupWindow = new AppPopupWindow();
        }
        return appPopupWindow;
    }

    public void initView(Context context, List<AppUtils.AppInfo> list){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.popupwindow_app, null);
            viewPager = inflate.findViewById(R.id.vp_app);
            ll_dot = inflate.findViewById(R.id.ll_dot);
            cl_bg = inflate.findViewById(R.id.cl_bg);
        }

        int pageCount = (int) Math.ceil(list.size() * 1.0 / pageSize);
        if(viewList == null){
            viewList = new ArrayList<>();
        }
        viewList.clear();

        for (int i = 0;i<pageCount;i++){
            GridView gridView = (GridView) LayoutInflater.from(mContext).inflate(R.layout.layout_grid_view, viewPager, false);
            gridView.setNumColumns(pageSize/2);
            gridView.setVerticalSpacing(DensityUtil.dip2px(mContext,20));
            gridView.setAdapter(new GridViewAdapter(context, list, i, pageSize));
            viewList.add(gridView);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int pos = position + currentIndex * pageSize;
                    RunningAppUtils.getInstance().init(context);
                    ArrayList<ApplicationBean> currentRunningApp = RunningAppUtils.getInstance().getCurrentRunningApp();
                    boolean isOpen = RunningAppUtils.getInstance().isOpen(list.get(pos).getPackageName());
                    LogUtils.getInstance().l(LogUtils.HINT,"aaaaaaaaaaaaaaaaaaaaaaaaa   isOpen : "+isOpen);
                    LogUtils.getInstance().l(LogUtils.HINT,"aaaaaaaaaaaaaaaaaaaaaaaaa   currentRunningApp.size() : "+currentRunningApp.size());
                    if(currentRunningApp.size()>=3 && !isOpen){
                        DialogCloseApkViewUtils.getInstance().initView(context,list.get(pos).getPackageName());
                    }else {
                        DeviceTypeUtils.getInstance().openApp(context,list.get(pos).getPackageName());
                    }
                    goneButton();
//                    BackButtonViewUtils.getInstance().setKillPackageName(list.get(pos).getPackageName());
//                Toast.makeText(context, list.get(pos).getAppname(), Toast.LENGTH_SHORT).show();
                }
            });
        }

        ll_dot.removeAllViews();

        for (int i = 0; i < pageCount; i++) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.view_dot, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            layoutParams.weight = 1;
            inflate.setLayoutParams(layoutParams);
            ll_dot.addView(inflate);
        }

        ll_dot.getChildAt(0).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.dot_selected);
        if (viewPagerAdapter == null){
            viewPagerAdapter = new ViewPagerAdapter(viewList);
        }
        viewPager.setAdapter(viewPagerAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {
                ll_dot.getChildAt(currentIndex).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.dot_normal);
                ll_dot.getChildAt(position).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.dot_selected);
                currentIndex = position;
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });

        cl_bg.setOnClickListener(this);


        if(layoutParams == null){
            layoutParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT
            );

            layoutParams.gravity = Gravity.CENTER;
        }


        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cl_bg:
              goneButton();
                break;
        }
    }

    public void goneButton(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
                HashMap<String, Object> map = new HashMap<>();
                map.put(EventBusEvent.IS_SHOW_APP,false);
                EventBus.getDefault().post(map);
            }
        }

    }

}
